package com.hongxin.nw.netty.mq_info;

import com.alibaba.fastjson.JSONObject;
import com.hongxin.nw.netty.model.Transfer;
import com.hongxin.nw.netty.model.analysis.info.DTmspna1;
import com.hongxin.nw.netty.mq_info.domain.CMD104;
import com.hongxin.nw.netty.utils.JSONUtils;
import com.hongxin.nw.netty.utils.ModifyJsonKeyUtil;
import com.hongxin.nw.netty.utils.pools.AsduStartPools;
import com.hongxin.nw.netty.values.Catalogue;
import com.hongxin.nw.netty.values.MqIdentif;
import com.hongxin.nw.netty.values.StaticValue;
import com.hongxin.nw.netty.values.TypeIdentif;

import java.util.ArrayList;
import java.util.List;

/**
 * @remark 充电中状态104
 * @author: GuoXiaolin
 * @date 2019/6/21 19:24
 * @Version 1.0
 * @Descibe
 */
public class ChargingStatus implements IMq {

    @SuppressWarnings("unchecked")
    @Override
    public JSONObject sendMqInfo(Transfer transfer) throws Exception {

        //json字段
        String jsonRemoveWorkingState= "workingState";//业务系统不需要的字段

        String code = transfer.getCode();
        List<DTmspna1> myl = (List<DTmspna1>) transfer.getObj();
        List<Integer> states = new ArrayList<>();
        for (DTmspna1 dataItem : myl) {
            if (StaticValue.QPS.equals(dataItem.getQds())) {//只发送qds == 1 的情况
                states.add(getMqNeeded(dataItem.getInfoAdr_i()));//将状态存字符串数组
            }
        }
        CMD104 cmd104 = new CMD104();
        cmd104.setInterfaceCount(String.valueOf(myl.get(0).getGun()+1));//充电口号
        cmd104.setMachineId(code);//充电桩编码,ASCII编码
        cmd104.setInterfaceNumber(AsduStartPools.getAsduStart(code).getNum());//充电枪数量
        cmd104.setInterfaceType(TypeIdentif.E_TYPE_DIRECT.getStr());//充电枪类型,2=直流； 1=交流；
        cmd104.setStateCount(String.valueOf(myl.size()));
        cmd104.setStates(states.toString());

        JSONObject json = (JSONObject) JSONObject.toJSON(cmd104);
        json.put(Catalogue.HXCMD, Catalogue.CMD104);
        JSONObject mqJson = ModifyJsonKeyUtil.removeEmptyKey(json);
        mqJson.remove(jsonRemoveWorkingState);

        //if (states.size() == 0){
        //    return JSONUtils.getRoot(-1,"直流告警信息为空,不发送MQ",StaticValue.VAL_NULL);
        //}
        return JSONUtils.getRoot(0, StaticValue.VAL_30, mqJson);
    }

    private Integer getMqNeeded(int i){
        Integer str = 0;
        switch (i){
            case 0:
                str = MqIdentif.REPORT_403.getInt();//"BMS通信异常";
                break;
            case 1:
                str = MqIdentif.REPORT_404.getInt();//"直流母线输出过压告警";
                break;
            case 2:
                str = MqIdentif.REPORT_405.getInt();//"直流母线输出欠压告警";
                break;
            case 3:
                str = MqIdentif.REPORT_426.getInt();//"充电机输入过压告警";
                break;
            case 4:
                str = MqIdentif.REPORT_427.getInt();//"充电机输入欠压告警";
                break;
            case 5:
                str = MqIdentif.REPORT_406.getInt();//"蓄电池充电过流告警";
                break;
            case 6:
                str = MqIdentif.REPORT_407.getInt();//"蓄电池模块采样点过温告警";
                break;
            case 7:
                str = MqIdentif.REPORT_408.getInt();//"是否连接电池";
                break;
            case 8:
                str = MqIdentif.REPORT_671.getInt();//"充电机连接器故障";
                break;
            case 9:
                str = MqIdentif.REPORT_700.getInt();//"BMS终止";
                break;
            case 10:
                str = MqIdentif.REPORT_63.getInt();//"充电机内部温度故障";
                break;
            case 11:
                str = MqIdentif.REPORT_63.getInt();//"温度过高保护";
                break;
            case 12:
                str = MqIdentif.REPORT_71.getInt();//"短路保护";
                break;
            case 13:
                str = MqIdentif.REPORT_208.getInt();//"漏电保护";
                break;
            case 14:
                str = MqIdentif.REPORT_504.getInt();//"急停开关触发";
                break;
        }
        return str;

    }
}
